<?php

	// +----------------------------------------------------------------------
	// | 店铺处理
	// +----------------------------------------------------------------------
	// | Copyright (c) 2015-2024 http://www.yicmf.com, All rights reserved.
	// +----------------------------------------------------------------------
	// | Author: 微尘 <yicmf@qq.com>
	// +----------------------------------------------------------------------

	namespace app\event\store;

	use app\common\event\Base;
    use app\model\im\Robot;
    use app\model\im\RobotTemplate;
    use app\model\ucenter\User as UserModel;
    use think\facade\Lang;
    use think\facade\Url;

	class StoreApp extends Base
	{

        /**
         * 通用更新方法
         * @param array   $param
         * @param UserModel $user
         * @return mixed
         * @author 微尘 <yicmf@qq.com>
         * @datetime: 2020/6/30 17:31
         */
        public function update($param, $user = null)
        {
            try {
                if (empty($param)) {
                    throw new Exception(Lang::get('__data_format_error_'));
                }
                if (!empty($param['id'])) {
                    // 更新操作
                    $result = $this->model::update($param, ['id'=>$param['id']]);
                } else { // 新增
                    $result = $this->model::create($param);


                    $robot = Robot::where('id',1)->find();

                    $insert = [
                        'store_id' => $user['store_id'],
                        'user_id' => $user['id'],
                        'store_app_id' => $result['id'],
                        'name' => $robot['name'],
                        'description' => $robot['description'],
                        'type' => $robot['type'],
                        'content_type' => $robot['content_type'],
                        'is_common' => $robot['is_common']]
                    ;
                    $insertRs =  Robot::create($insert);
                    // 新增自动消息默认模板
                    $tem = \app\model\im\RobotTemplate::where('store_app_id',null)
                        ->field('name,description,type,content_type,reply,is_common,delay,sort,match_type,status')
                        ->where('status',1)->select()->toArray();


                    $saveAll = [];
                    foreach ($tem as $item) {
                        $item['robot_id'] = $insertRs['id'];
                        $item['store_id'] = $user['store_id'];
                        $item['user_id'] = $user['id'];
                        $item['store_app_id'] = $result['id'];
                        $saveAll[] = $item;
                    }
                    $model = new RobotTemplate();
                    $model->saveAll($saveAll);

                }
                $data['code'] = 0;
                $data['data'] = $result;
                $data['message'] = '操作成功';
            } catch (\Exception $e) {
                $data['code'] = 1;
                $data['message'] = $e->getMessage();
            }
            return $data;
        }

	}
